package com.xh.jf_blog.common.util;

import com.xh.jf_blog.bean.Userinfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
 * @Author PangJunjie
 * @Date 2024/2/1/001
 * <p>
 * <p>
 * 在SpringMVC声明周期内继承该类，可以获得session
 */
public abstract class SessionInfo {
    protected Userinfo getAdminInfo() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        Object obj = session.getAttribute("loginInfo");
        if (obj != null) {
            return (Userinfo) obj;
        }
        return null;
    }

    protected Integer getAdminId() {
        if (getAdminInfo() != null) {
            return getAdminInfo().getUserinfoId();
        }
        return null;
    }

    protected Userinfo getVipInfo() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = attributes.getRequest().getSession();
        Object obj = session.getAttribute("vipInfo");
        if (obj != null) {
            return (Userinfo) obj;
        }
        return null;
    }

    protected Integer getVipId() {
        if (getVipInfo() != null) {
            return getVipInfo().getUserinfoId();
        }
        return null;
    }
}
